package com.gc.controller;

import com.gc.common.constants.FileConstants;
import com.gc.common.constants.SysConstants;
import com.gc.common.resp.ApiResult;
import com.gc.common.util.FileUtils;
import com.gc.common.util.MyUtil;
import com.gc.dto.UserDTO;
import com.gc.pojo.SysUser;
import com.gc.services.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ClientService clientService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private TemplateService templateService;

    @RequestMapping("add-view")
    public ModelAndView addUserView() {
        if (!checkRole()) {
            return null;
        }
        ModelAndView mav = new ModelAndView("user/add");
        mav.addObject("clients", clientService.getAllClient());
        mav.addObject("roles", roleService.getAll());
        mav.addObject("devices", deviceService.getAllDevice());
        mav.addObject("templates", templateService.getAll());
        return mav;
    }


    @RequestMapping("add")
    @ResponseBody
    public ApiResult addUser(@RequestBody UserDTO user) {
        if (!checkRole()) {
            return null;
        }
        SysUser u =  userService.getUserByLoginName(user.getLoginName());
        if(!ObjectUtils.isEmpty(u)){
            return new ApiResult(200,"登录名不能重复！");
        }
        String sinUrl = "";
        if (!StringUtils.isEmpty(user.getSignature())) {
            sinUrl = FileConstants.SIGNATURE_IMG_VIRTUAL_URL + MyUtil.Base64ToImage(user.getSignature(), FileConstants.SIGNATURE_IMG_LOCAL_URL);
        }
        user.setSignature(sinUrl);
        int count = userService.addUser(user);
        return new ApiResult();
    }

    @RequestMapping("modify")
    @ResponseBody
    public ApiResult modifyUser(@RequestBody UserDTO user) {
        if (!checkRole()) {
            return null;
        }
        SysUser u =  userService.getUserByLoginName(user.getLoginName());
        if(!ObjectUtils.isEmpty(u) && !u.getUserId().equals(user.getUserId())){
            return new ApiResult(200,"登录名不能重复！");
        }
        String sinUrl;
        if (!StringUtils.isEmpty(user.getSignature())) {
            sinUrl = FileConstants.SIGNATURE_IMG_VIRTUAL_URL + MyUtil.Base64ToImage(user.getSignature(), FileConstants.SIGNATURE_IMG_LOCAL_URL);
            user.setSignature(sinUrl);
        }
        int count = userService.modifyUser(user);
        return new ApiResult();
    }


    @RequestMapping("getAll")
    @ResponseBody
    public List<UserDTO> getAll() {
        if (!checkRole()) {
            return null;
        }
        List<UserDTO> list = userService.getAll();
        return list;
    }

    @RequestMapping("get")
    @ResponseBody
    public UserDTO get(@RequestParam Integer userId) {
        if (!checkRole()) {
            return null;
        }
        return userService.getUserDTOByUId(userId);
    }


    @RequestMapping("remove")
    @ResponseBody
    public ApiResult remove(@RequestParam Integer userId) {
        if (!checkRole()) {
            return null;
        }
        int count1 = userService.remove(userId);
        return new ApiResult();
    }

    private boolean checkRole() {
        SysUser curUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        Subject subject = SecurityUtils.getSubject();
        return subject.hasRole(SysConstants.SYS_ROLE1);
    }
}
